package com.tuanbiao.presenter;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.abiao.lib_utils.NetworkUtils;
import com.amap.api.maps.model.LatLng;
import com.hzjq.data.entity.BlastLonLat;
import com.hzjq.lib.BlastKool;
import com.hzjq.lib.location.LocationLoader;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.LocationController;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/24 11:18
 */
public class LocationPresenter extends BasePresenter<LocationController.Model, LocationController.View> implements LocationController.Presenter, LocationLoader.OnLocationChangeListener {
    private BlastLonLat currentLocation;

    public LocationPresenter(LocationController.Model model, LocationController.View rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void startLocate() {
        mRootView.showProjectDetails(mModel.getCurrentProject());
        NetworkUtils.registerNetworkStatusChangedListener(new NetworkUtils.OnNetworkStatusChangedListener() {
            @Override
            public void onDisconnected() {
                Timber.i("onDisconnected");
                BlastKool.Companion.loader()
                        .location().removeLocationChangeListener(LocationPresenter.this);
            }

            @Override
            public void onConnected(NetworkUtils.NetworkType networkType) {
                Timber.i("onConnected");
                BlastKool.Companion.loader()
                        .location()
                        .addLocationChangeListener(LocationPresenter.this)
                        .start();
            }
        });

    }
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void startLocation() {
        BlastKool.Companion.loader()
                .location()
                .addLocationChangeListener(this)
                .start();
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private void stopLocation() {
        BlastKool.Companion.loader().location().removeLocationChangeListener(this);
    }

//    @Override
//    public void onDestroy() {
//        BlastKool.Companion.loader()
//                .location()
//                .onDestroy();
//        super.onDestroy();
//    }

    @Override
    public float calculateLineDistance(LatLng point1, LatLng point2) {
        return mModel.calculateLineDistance(point1, point2);
    }

    @Override
    public void saveCurrentLocation() {
        if (null != currentLocation) {
            mModel.saveMarkPoint(currentLocation);
        }
    }

    @Override
    public void onLocationChanged(@NonNull BlastLonLat blastLonLat) {
        Timber.i("onLocationChanged:" + blastLonLat+Thread.currentThread().getName());
        post(new Runnable() {
            @Override
            public void run() {
                Timber.i("onLocationChanged:run:" + blastLonLat+Thread.currentThread().getName());
                if (0 != blastLonLat.getLat() && 0 != blastLonLat.getLon()) {
                    if (null != mRootView)
                        mRootView.hideLoading();
                }
                if (null != mRootView)
                    mRootView.showLonLat(blastLonLat);
            }
        });

    }
}
